package com.kaishengit.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.kaishengit.dao.CommentDao;
import com.kaishengit.dao.MessageDao;
import com.kaishengit.entity.Comment;
import com.kaishengit.entity.Message;
import com.kaishengit.entity.User;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKutil;

public class CommentService {
	private CommentDao dao = new CommentDao();
	private MessageDao mdao=new MessageDao();
	private Comment comm=new Comment();
	
	public List<Comment> findAllByDocumentid(String id){
		List<Comment> list = dao.findByDocumentId(id);
		return list;
	}
	
	/**
	 * 保存资料的评论
	 * */
	public void save(User user, String content, String documentId) {
		
		 //获取资料所对应的messageid;
		Message  m=mdao.findByDocumentId(documentId);
		String mid=m.getId();
		
		String createTime = DateUtil.getNow();
		String commentId = PKutil.getPk();
		Comment comment = new Comment();
		comment.setContent(content);
		comment.setCreatetime(createTime);
		comment.setDocumentid(documentId);
		comment.setMessageid(mid);
		comment.setId(commentId);
		comment.setUserid(user.getId());
		dao.save(comment);
		
		
		
	}
	public List<Comment> findByideaid(String ideaid) {
		List<Comment> list=dao.findByIdeaId(ideaid);
		List<Comment> newList=new ArrayList<Comment>();
		for(Comment com:list){
			String time;
			try {
				time=DateUtil.getAccount(DateUtil.getNow(), com.getCreatetime());
				com.setCreatetime(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			newList.add(com);
		}
		return newList;
	}
	
	/**
	 * 保存想法的评论
	 * */
	public void saveIdea(String userid, String content, String ideaId) {
		
		//获取想法对应的messageid
		Message  m=mdao.findByIdeaId(ideaId);
		String imid=m.getId();
		
		
		String createTime = DateUtil.getNow();
		String commentId = PKutil.getPk();
		Comment comment = new Comment();
		comment.setContent(content);
		comment.setCreatetime(createTime);
		comment.setMessageid(imid);
		comment.setIdeaid(ideaId);
		comment.setId(commentId);
		comment.setUserid(userid);
		dao.saveIdea(comment);
		
		
	}

	public void save(String messageid, String content, String userid) {
		String time=DateUtil.getNow();
		String id=PKutil.getPk();
		comm.setId(id);
		comm.setContent(content);
		comm.setCreatetime(time);
		comm.setMessageid(messageid);
		comm.setUserid(userid);
		dao.saveInfor(comm);
	}

	public void delByDocumentId(String id) {
		dao.delByDocumentId(id);
		
	}

	
	
}
